import {Injectable} from '@angular/core';
import {Education} from '../types/resp/Education';
import {EducationForm} from '../types/req/EducationForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {EducationQueryForm} from '../types/req/EducationQueryForm';

@Injectable({
  providedIn: 'root'
})
export class EducationService {
  educations: Education[] = [
    {"id":1,"candidateId":3,"candidateName":"张伟","schoolName":"清华大学","degree":30,"startDate":"2008-09-01","endDate":"2012-06-30","createTime":"2024-01-26 22:24:11","modifyTime":"2024-07-25 22:24:11"},
    {"id":2,"candidateId":1,"candidateName":"王强","schoolName":"上海交通大学","degree":40,"startDate":"2010-09-01","endDate":"2013-06-30","createTime":"2024-04-21 22:24:11","modifyTime":"2024-01-21 22:24:11"},
    {"id":3,"candidateId":3,"candidateName":"张伟","schoolName":"北京大学","degree":40,"startDate":"2012-09-01","endDate":"2015-06-30","createTime":"2024-11-12 22:24:11","modifyTime":"2024-06-21 22:24:11"},
    {"id":4,"candidateId":2,"candidateName":"陈静","schoolName":"浙江大学","degree":10,"startDate":"2009-09-01","endDate":"2012-06-30","createTime":"2025-01-04 22:24:11","modifyTime":"2024-12-08 22:24:11"},
    {"id":5,"candidateId":2,"candidateName":"陈静","schoolName":"复旦大学","degree":40,"startDate":"2012-09-01","endDate":"2015-06-30","createTime":"2024-10-02 22:24:11","modifyTime":"2024-06-25 22:24:11"},
    {"id":6,"candidateId":1,"candidateName":"王强","schoolName":"南京大学","degree":50,"startDate":"2013-09-01","endDate":"2018-06-30","createTime":"2024-09-01 22:24:11","modifyTime":"2024-04-25 22:24:11"},
    {"id":7,"candidateId":3,"candidateName":"张伟","schoolName":"中国人民大学","degree":20,"startDate":"2015-09-01","endDate":"2017-06-30","createTime":"2024-03-23 22:24:11","modifyTime":"2024-05-30 22:24:11"}
  ]
  constructor() {
  }

  save (educationForm: EducationForm): Observable<Result<void>> {
    console.log('educationApi save params', educationForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (educationQueryForm: EducationQueryForm): Observable<Result<Page<Education>>> {
    console.log('educationApi find params', educationQueryForm)
    let filteredEducations = this.educations
    if (educationQueryForm.candidateId) {
      filteredEducations = this.educations.filter(education => education.candidateId === educationQueryForm.candidateId);
    }
    return MockApi.queryPageSuccessfully(filteredEducations).pipe(
      map((response: Response<Result<Page<Education>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Education>> {
    console.log('educationApi findById id', id)
    let target = {}
    for (const a of this.educations) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Education>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('educationApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
